package com.ws.eduorder.controller;


import com.ws.commonutils.R;
import com.ws.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-12-14
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    //通过订单号获取微信支付二维码
    @PostMapping("getNative/{orderNo}")
    public R getNative(@PathVariable("orderNo") String orderNo) {
        Map map=payLogService.getNative(orderNo);

        return R.ok().data(map);
    }

    @GetMapping("getPayStatus/{orderNo}")
    public R getPayStatus(@PathVariable("orderNo") String orderNo) {
        Map<String,String> map=payLogService.getPayStatus(orderNo);
        //模拟支付成功，手动设置一下
        map.put("trade_state","SUCCESS");
        System.out.println(map);
        if (map==null){
            return R.error().message("支付出错");
        }

        if (map.get("trade_state").equals("SUCCESS")){
            payLogService.updatePayStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }

}

